//******************************************************************************
// @File: cyzRunAction.cc
// @Author: Cyz
// @Date: 2020年 5月 4日 星期一 15时47分43秒 CST
// @Description: 
//		Implementation of the cyzRunAction class
//******************************************************************************

#include "cyzRunAction.hh"
#include "cyzAnalysis.hh"

#include "G4Run.hh"
#include "G4RunManager.hh"
#include "G4UnitsTable.hh"
#include "G4SystemOfUnits.hh"

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.....

cyzRunAction::cyzRunAction() : G4UserRunAction()
{
	//
	// Set printing event number per each event 
	//
	G4RunManager::GetRunManager()->SetPrintProgress(1); 

	// 
	// Create analysis manager
	// Interface with root, setted in cyzAnalysis.hh
	//
	auto analysisManager = G4AnalysisManager::Instance();
	G4cout << "Using" << analysisManager->GetType() << G4endl;

	//Create directories
	//analysisManager->SetHistoDirectoryName("histograms"); // Why didn't use?

	analysisManager->SetVerboseLevel(1);

	//
	// Creating Ntuple
	//
	analysisManager->CreateNtuple("cyz","Data");
	/********  EvnetID  ********/
	analysisManager->CreateNtupleIColumn("eventID");
	
	/********  Initial Diraction of Momentum  ********/
	analysisManager->CreateNtupleDColumn("init_Direction_phi");// ColumnID 1
	analysisManager->CreateNtupleDColumn("init_Direction_cosTheta");
	
	/********  Energy Deposition  ********/
	analysisManager->CreateNtupleDColumn("edep_H");            // ColumnID 3
	analysisManager->CreateNtupleDColumn("edep_B");
	
	/********  Center of Gravity of Energy Depsition  ********/
	analysisManager->CreateNtupleDColumn("center_H_x");        // ColumnID 5
	analysisManager->CreateNtupleDColumn("center_H_y");
	analysisManager->CreateNtupleDColumn("center_H_z");

	analysisManager->CreateNtupleDColumn("center_B_x");        // ColumnID 8
	analysisManager->CreateNtupleDColumn("center_B_y");
	analysisManager->CreateNtupleDColumn("center_B_z");
	
	//
	// Finish
	//
	analysisManager->FinishNtuple();
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

cyzRunAction::~cyzRunAction()
{
	delete G4AnalysisManager::Instance();
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

void cyzRunAction::BeginOfRunAction(const G4Run* /*run*/)
{
	// Get analysis manager 
	auto analysisManager = G4AnalysisManager::Instance();

	// Open an output file 
	analysisManager->OpenFile("cyz_data");
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.....

void cyzRunAction::EndOfRunAction(const G4Run* /*run*/)
{
	// print histogram statistics
	auto analysisManager = G4AnalysisManager::Instance();
	
	// save histograms
	analysisManager->Write();
	analysisManager->CloseFile();
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.....
